package com.imooc.sell.controller;

import com.imooc.sell.dto.OrderDTO;
import com.imooc.sell.enums.ResultEnum;
import com.imooc.sell.exception.SellException;
import com.imooc.sell.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/seller/order")
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                             @RequestParam(value = "size",defaultValue = "10") Integer size,
                             Map<String,Object> map){
        PageRequest pageRequest = new PageRequest(page-1,size);
        Page<OrderDTO> list = orderService.findList(pageRequest);

        map.put("orderDTOPage",list);
        map.put("currentPage",page);
        return new ModelAndView("order/list",map);
    }

    @GetMapping("cancel")
    public ModelAndView cancel(@RequestParam(value = "orderId") String orderId,
                               Map<String,Object> map){
        try {
            OrderDTO orderDTO = orderService.findOne(orderId);
            if (orderDTO==null){
                log.error("【卖家端取消订单】订单不存在,orderId={}",orderId);

                getMap(ResultEnum.ORDER_NOT_EXIST.getMessage(),map);
                return new ModelAndView("common/error",map);
            }
            orderService.cancel(orderDTO);
        } catch (SellException e) {
            getMap(e.getMessage(),map);
            return new ModelAndView("common/error",map);
        }

        getMap(ResultEnum.ORDER_CANCEL_SUCCESS.getMessage(),map);
        return new ModelAndView("common/success",map);
    }

    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "orderId")String orderId,
                               Map<String,Object> map){
        OrderDTO orderDTO = null;
        try {
             orderDTO = orderService.findOne(orderId);
            if (orderDTO==null){
                log.error("【卖家端查看订单】订单不存在,orderId={}",orderId);

                getMap(ResultEnum.ORDER_NOT_EXIST.getMessage(),map);
                return new ModelAndView("common/error",map);
            }
        } catch (SellException e) {
            getMap(e.getMessage(),map);
            return new ModelAndView("common/error",map);
        }
        map.put("orderDTO",orderDTO);
        return new ModelAndView("order/detail",map);
    }

    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam(value = "orderId")String orderId,
                               Map<String,Object> map){
        try {
            OrderDTO  orderDTO = orderService.findOne(orderId);
            if (orderDTO==null){
                log.error("【卖家端完结订单】订单不存在,orderId={}",orderId);

                getMap(ResultEnum.ORDER_NOT_EXIST.getMessage(),map);
                return new ModelAndView("common/error",map);
            }
            orderService.finish(orderDTO);
        } catch (SellException e) {
            getMap(e.getMessage(),map);
            return new ModelAndView("common/error",map);
        }
        getMap(ResultEnum.ORDER_FINISH_SUCCESS.getMessage(),map);

        return new ModelAndView("common/success",map);
    }


    private Map<String,Object> getMap(String message,Map<String,Object> map){
        map.put("msg",message);
        map.put("url","/seller/order/list");
        return map;
    }
}
